//
//  XRProgressSlider.m
//  avplayer
//
//  Created by 谢汝 on 2017/12/15.
//  Copyright © 2017年 谢汝. All rights reserved.
//

#import "XRProgressSlider.h"

#define RGBColor(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a];

@interface XRProgressSlider ()

@property (nonatomic, strong) UIColor *lineColor;
@property (nonatomic, strong) UIColor *slidedLineColor;
@property (nonatomic, strong) UIColor *bufferLineColor;//预加载线的颜色
@property (nonatomic, strong) UIColor *circleColor;//圆的颜色

@property (nonatomic, assign) CGFloat lineWidth;//线的宽度
@property (nonatomic, assign) CGFloat circleRadius;//圆的半径

@end

@implementation XRProgressSlider


- (id)initWithFrame:(CGRect)frame direction:(XRProgressSliderDirection)direction {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        
        _minValue = 0;
        _maxValue = 1;
        
        _direction = direction;
        _lineColor = [UIColor whiteColor];
        _slidedLineColor = RGBColor(254, 64, 22, 1);
        _circleColor = RGBColor(254, 64, 22, 1);
        
        _bufferLineColor = [UIColor grayColor];
        
        _sliderPercent = 0.0;
        _lineWidth = 2;
        _circleRadius = 8;
    
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //画总体的线
    CGContextSetStrokeColorWithColor(context, _lineColor.CGColor); //画笔颜色
    CGContextSetLineWidth(context, _lineWidth); //线的颜色
    
    CGFloat startLineX = (_direction == XRProgressSliderDirectionHorizonal)?_circleRadius:(self.frame.size.width - _lineWidth)/2;
    CGFloat startLineY = (_direction == XRProgressSliderDirectionHorizonal)?(self.frame.size.height - _lineWidth)/2:_circleRadius;
    
    CGFloat endLineX = self.frame.size.width - _circleRadius;
    CGFloat endLineY = startLineY;
    
    
    CGContextMoveToPoint(context, startLineX, startLineY);
    CGContextAddLineToPoint(context, endLineX, endLineY);
    CGContextClosePath(context);
    CGContextStrokePath(context);
    
    //画缓冲的线
    CGContextSetStrokeColorWithColor(context, _bufferLineColor.CGColor);
    CGContextSetLineWidth(context, _lineWidth);
    
    CGFloat bufferLineX = MAX(_circleRadius, _bufferPercent * (self.frame.size.width - _circleRadius));
    CGFloat bufferLineY = startLineY;
    
    CGContextMoveToPoint(context, startLineX, startLineY);
    CGContextAddLineToPoint(context, bufferLineX, bufferLineY);
    CGContextClosePath(context);
    CGContextStrokePath(context);
    
    
    //画已滑动进度的线
    CGContextSetStrokeColorWithColor(context, _slidedLineColor.CGColor);
    CGContextSetLineWidth(context, _lineWidth);
    
    CGFloat slidedLineX = MAX(_circleRadius, _sliderPercent * (self.frame.size.width - 2 * _circleRadius) + _circleRadius);
    CGFloat slidedLineY = startLineY;
    CGContextMoveToPoint(context, startLineX, startLineY);
    CGContextAddLineToPoint(context, slidedLineX, slidedLineY);
    CGContextClosePath(context);
    CGContextStrokePath(context);
    
    
    //外层圆
    CGFloat penWidth = 1.f;
    CGContextSetStrokeColorWithColor(context, _circleColor.CGColor);
    CGContextSetLineWidth(context, penWidth);
    
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextSetShadow(context, CGSizeMake(1, 1), 1.f);

    CGFloat circleX = _circleRadius + penWidth;
    CGFloat circleY = startLineY;
    CGContextAddArc(context, circleX, circleY, _circleRadius, 0, 2 * M_PI, 0); //添加一个圆
    CGContextDrawPath(context, kCGPathFillStroke); //绘制路径 加填充
    
    //内层圆
    
    CGContextSetStrokeColorWithColor(context, _circleColor.CGColor);
    CGContextSetLineWidth(context, 0);
    CGContextSetFillColorWithColor(context, _circleColor.CGColor);
    CGContextAddArc(context, circleX, circleY, _circleRadius / 2, 0, 2 * M_PI * 2, 0);
    CGContextDrawPath(context, kCGPathFillStroke);
    
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if(!self.enabled){
        return;
    }
    
    [self updateTouchPoint:touches];
    [self callbackTouchEnd:NO];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (!self.enabled) {
        return;
    }
    
    [self updateTouchPoint:touches];
    [self callbackTouchEnd:NO];
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (!self.enabled) {
        return;
    }
    [self updateTouchPoint:touches];
    [self callbackTouchEnd:YES];
}


- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (!self.enabled) {
        return;
    }
    [self updateTouchPoint:touches];
    [self callbackTouchEnd:YES];
}


- (void)updateTouchPoint:(NSSet *)touches {
    CGPoint touchPoint = [[touches anyObject] locationInView:self];
    if (_direction == XRProgressSliderDirectionHorizonal) {
        self.sliderPercent = touchPoint.x / self.frame.size.width;
    }else {
        self.sliderPercent = touchPoint.y / self.frame.size.height;
    }
    
    
}


- (void)setSliderPercent:(CGFloat)sliderPercent {
    if (_sliderPercent != sliderPercent) {
        _sliderPercent = sliderPercent;
        self.value = _minValue + sliderPercent * (_maxValue - _minValue);
    }
    
}

- (void)setBufferPercent:(CGFloat)bufferPercent {
    if (_bufferPercent != bufferPercent) {
        _bufferPercent = bufferPercent;
        [self setNeedsDisplay];
    }
}
    
- (void)setValue:(CGFloat)value {
    if (_value != value) {
        if (value < _minValue) {
            _value = _minValue;
            return;
        }else if (value > _maxValue){
            _value = _maxValue;
            return;
        }
        _value = value;
        
        _sliderPercent = (_value - _minValue)/(_maxValue - _minValue);
        [self setNeedsDisplay];
    }
}


- (void)callbackTouchEnd:(BOOL)isTouchEnd {
    _isSliding = !isTouchEnd;
    if (isTouchEnd) {
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
}

@end
